package com.mew.slots.core.game.sgj.room.bouns;


import com.mew.common.util.GameUtil;
import com.mew.slots.core.game.sgj.roller.Marryili;
import com.mew.slots.core.game.sgj.roller.SgjIli;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 每种水果出现概率
 */

@Data
@ToString(callSuper = true)
@NoArgsConstructor
public class FruitMarryRand {

    private static final Logger logger = LoggerFactory.getLogger(FruitMarryRand.class);

    //0香蕉 //1西瓜//2芒果//3葡萄//4菠萝//5铃铛//6樱桃//7Bar//8橙子//9贝壳 //10scatter
    //会出现的水果
    int[] Fruit = {4,8,0,6,2,3,1};
    //外圈为哪个水果中奖   香蕉 10，橘子 24，菠萝 65，樱桃 1，芒果葡萄西瓜0
    int[] FruitMaryRand = {61,19,20,0};
    //连续相同
    int[] SameFruit = {80,100,100,100,100};   //1  90,2   95,,    3不连  99,3 连   100,4   0
    //中奖概率
    int[][] MaryRand =
            { //炸弹，未中奖，中奖，
                    {30,50,100},
                    {10,40,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
                    {100,100,100},
            };

    public FruitMarryRand(int rand){

        Marryili marryili = SgjIli.getInstance().getBouns().stream().collect(Collectors.toMap(Marryili::getId, i -> i)).get(rand);

        if (null != marryili){
            FruitMaryRand = marryili.getFruitMaryRand();
            MaryRand = marryili.getMaryRand();
            SameFruit = marryili.getSameFruit();
            logger.info("xml:"+rand);
        }

    };
    private Random random = new Random();

    public MarryMsgs getMarryRes(int num) {

        StringBuilder builder = new StringBuilder();
        builder.append("time:"+(num+1)+" ");
        MarryMsgs res = new MarryMsgs();
        //所有图标 乱序
        List<Integer> fruitslist = Arrays.stream(Fruit).boxed().collect(Collectors.toList());
        Collections.shuffle(fruitslist);

        //判断是否中奖   0-99
        int win = (int) (Math.random()*100);
        builder.append(" w:"+win);
        //超过最大次数
        if(num>=MaryRand.length){
            num = MaryRand.length-1;
        }
        if(win<MaryRand[num][0]){ //炸弹
            res.center = fruitslist.stream().limit(4).mapToInt(Integer::intValue).toArray();
            res.icon = -1;
            res.type = -1;
        }else if( win<MaryRand[num][1] ){//未中奖
            res.center = fruitslist.stream().limit(4).mapToInt(Integer::intValue).toArray();
            res.icon = fruitslist.get(4);
            res.type = 0;
        }else if(win<MaryRand[num][2]){//中奖
            //中的加到中间  从全部类型中移除
            res.icon = Fruit[GameUtil.weights(FruitMaryRand)];
            fruitslist.remove((Integer) res.icon);
            ArrayList<Integer> centerList = new ArrayList<Integer>();
            //几个相同
            int same = (int)(Math.random()*100);
            builder.append(" same:"+same);
            if(same<SameFruit[0]){//一个
                res.type = 1;
                centerList.add(res.icon);
                for (int i = 0; centerList.size() < 4; i++) {
                    centerList.add(fruitslist.get(i));
                }
                Collections.shuffle(centerList);
            }else if(same<SameFruit[1]){//两个
                res.type = 2;
                centerList.add(res.icon);
                centerList.add(res.icon);
                for (int i = 0; centerList.size() < 4; i++) {
                    centerList.add(fruitslist.get(i));
                }
                Collections.shuffle(centerList);
            }else if(same<SameFruit[2]){//3个  不连  99   1101   1011
                res.type = 3;
                centerList.add(res.icon);
                centerList.add(res.icon);
                centerList.add(res.icon);
                centerList.add(res.icon);
                //索引1 或2替换
                int seat = (int) (Math.random()*2)+1;
                centerList.set(seat,fruitslist.get(0));

            }else if(same<SameFruit[3]){//3个  连  99   1101   1011
                res.type = 4;
                centerList.add(res.icon);
                centerList.add(res.icon);
                centerList.add(res.icon);
                centerList.add(res.icon);
                //索引0 或3替换
                int seat = (int) (Math.random()*100);
                if (seat<50){
                    centerList.set(0,fruitslist.get(0));
                }else {
                    centerList.set(3,fruitslist.get(0));
                }
            }
            res.center = centerList.stream().mapToInt(Integer::intValue).toArray();
        }else {
            logger.info("roll to max");
            return null;
        }

        System.out.println(builder.toString()+" res:"+res);
        return res;
    }

}
